package xiaohong.data.tree.binary;

import lombok.Data;

@Data
public class TreeNode {

    //节点的权
    int value;

    String name;
    //设置左儿子
    //左儿子
    TreeNode leftNode;

    //设置右儿子
    //右儿子
    TreeNode rightNode;

    public TreeNode() {
    }

    public TreeNode(int value , String name) {
        this.value = value;
        this.name = name;
    }


    public void frontShow() {
        System.out.println(this);
        if (leftNode != null) {
            leftNode.frontShow();
        }
        if (rightNode != null) {
            rightNode.frontShow();
        }
    }

    public void middleShow() {
        if (leftNode != null) {
            leftNode.middleShow();
        }
        System.out.println(this);
        if (rightNode != null) {
            rightNode.middleShow();
        }
    }

    public void afterShow() {
        if (leftNode != null) {
            leftNode.afterShow();
        }
        if (rightNode != null) {
            rightNode.afterShow();
        }
        //先遍历当前节点的值
        System.out.println(this);
    }


    //先序查找
    public TreeNode frontSearch(int i) {
        TreeNode target = null;
        //对比当前节点的值
        if (this.value == i) {
            return this;
            //当前节点不是要查找的节点
        } else {
            //查找左儿子
            if (leftNode != null) {
                //查找的话t赋值给target，查不到target还是null
                target = leftNode.frontSearch(i);
            }
            //如果target不为空，说明在左儿子中已经找到
            if (target != null) {
                return target;
            }
            //如果左儿子没有查到，再查找右儿子
            if (rightNode != null) {
                target = rightNode.frontSearch(i);
            }
        }
        return target;
    }

    // 中序查找
    public TreeNode middleSearch(int i) {
        TreeNode target = null;
        if (leftNode != null) {
            target = this.leftNode.middleSearch(i);
        }
        if (target != null) {
            return target;
        }
        if (this.value == i) {
            return this;
        }
        if (rightNode != null) {
            target = rightNode.middleSearch(i);
        }
        return target;
    }

    // 后序查找
    public TreeNode afterSearch(int i) {
        TreeNode target = null;
        if (leftNode != null) {
            target = this.leftNode.afterSearch(i);
        }
        if (target != null) {
            return target;
        }
        if (rightNode != null) {
            target = rightNode.afterSearch(i);
        }
        if (target != null) {
            return target;
        }
        if (this.value == i) {
            return this;
        }
        return target;
    }

    public void delete(int i) {
        if (this.leftNode != null && this.leftNode.value == i) {
            this.leftNode = null;
            return;
        }
        if (this.rightNode != null && this.rightNode.value == i) {
            this.rightNode = null;
        }
        if(this.leftNode != null){
            this.leftNode.delete(i);
        }
        if(this.rightNode != null){
            this.rightNode.delete(i);
        }
    }


    @Override
    public String toString() {
        return "TreeNode{" +
                "name='" + name + '\'' +
                ", value=" + value +
                '}';
    }
}